package cn.edu.lzu.dao;

import cn.edu.lzu.entity.Role;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @PackageName: cn.edu.lzu.dao
 * @InterfaceName: RoleMapper
 * @Description: TODO
 * @Author: 张琦[QQ:3098086691]
 * @Date: 2020/5/6 10:54
 * @Version: 1.0
 */
@Mapper
public interface RoleMapper {
    @Select("select * from role where is_deleted=0 and unit_id=#{unitId}")
    List<Role> selRolesTrue(long unitId);

    @Delete("delete from role_permission where role_id in (${roleIds})")
    int delRolePerms(String roleIds);

    @Update("update role set is_deleted=1 where role_id in (${roleIds})")
    int updDeleteRole(String roleIds);

    @Insert("insert into role (role_id, role, role_name, unit_id, is_deleted) values (default, #{role}, #{roleName}, #{unitId}, 0)")
    int insRole(String role, String roleName, long unitId);

    @Insert("insert into role_permission (id, role_id, permission_id, is_deleted) values (default, #{roleId}, #{permissionId}, 0)")
    int insRolePermission(int roleId, int permissionId);

    @Select("select * from role where (role_name=#{roleName} or role=#{role}) and is_deleted=0 and unit_id=#{unitId}")
    Role selByRoleAndName(String role, String roleName, long unitId);

    @Select("select * from role where role_id=#{roleId}")
    Role selByRoleId(int roleId);

    @Select("select permission_id from role_permission where role_id=#{roleId} and is_deleted=0")
    List<Integer> selPermissionIdsByRoleId(int roleId);

    @Update("update role set role=#{role}, role_name=#{roleName} where role_id=#{roleId}")
    int updRoleAndName(Role role);

    @Select("select role_id from examiner_role where examiner_id=#{examinerId} and is_deleted=0")
    List<Integer> selRolesByExaminerId(long examinerId);

    @Select("select role_id from examiner_role where examiner_id=#{examinerId} and is_deleted=0")
    List<Integer> selRoleIdsByExaminerId(long examinerId);

    @Select("select * from role where is_deleted=0 and unit_id=#{unitId}")
    List<Role> selRolesByUnitId(long unitId);

    @Delete("delete from examiner_role where examiner_id=#{examinerId}")
    int delRoleByExaminerId(long examinerId);

    @Insert("insert into examiner_role (id, examiner_id, role_id, is_deleted) values (default, #{examinerId}, #{roleId}, 0)")
    int insExaminerRole(long examinerId, int roleId);

    @Select("select * from role where role_id in (select role_id from examiner_role where is_deleted=0 and examiner_id=#{examinerId})")
    List<Role> selExaminerRoles(long examinerId);


    @Update("update role_permission set is_deleted=1 where role_id in (${roleIds})")
    int updDeleteRolePerms(String roleIds);
}
